// Used to store that is shared between the functions/chapters of a mission.

//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "management\fn_setMissionJIPVar.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_key","_value","_instance_id","_result","_variables","_instances"];
DEFAULT_PARAM(_instance_id,0,"");
DEFAULT_PARAM(_key,1,"");
DEFAULT_PARAM(_value,2,nil);

if (isnil QGVAR(STORING_MISSION_VAR_BUSY)) then { GVAR(STORING_MISSION_VAR_BUSY) = -1; };
waituntil {(GVAR(STORING_MISSION_VAR_BUSY) < time)};
GVAR(STORING_MISSION_VAR_BUSY) = time + 10;

if (_key != "") then {
	_instances = JIPGETVAR(QGVAR(MISSION_INSTANCES),[]);
	if (!(_instance_id in _instances)) then {
		_instances set [count _instances, _instance_id];
		JIPSETVAR(QGVAR(MISSION_INSTANCES),_instances);
	};
	_variables = JIPGETVAR(_instance_id,(call CFUNC(hashEmpty)));
	_variables = [_variables,_key,_value] call CFUNC(hashSet);
	JIPSETVAR(_instance_id,_variables);
};

GVAR(STORING_MISSION_VAR_BUSY) = -1;
TRACE("End");
TMFPROFILERSTOP;
